package com.project.school_flate.serviceimpl.com;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.project.school_flate.dto.com.ComDistributionRuleDto;
import com.project.school_flate.entity.com.ComDistributionRule;
import com.project.school_flate.entity.com.table.ComDistributionRuleTableDef;
import com.project.school_flate.mapper.com.ComDistributionRuleMapper;
import com.project.school_flate.service.com.ComDistributionRuleService;
import com.project.school_flate.util.PoToDTO;
import com.project.school_flate.util.Result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务层实现。
 *
 * @author 马维健
 * @since 2024/1/2
 */
@Service
public class ComDistributionRuleServiceImpl extends ServiceImpl<ComDistributionRuleMapper, ComDistributionRule> implements ComDistributionRuleService {

    @Autowired
    private ComDistributionRuleMapper oComDistributionRuleMapper;

    /**
     * 获取分销商等级规则
     * @param oComDistributionRuleDto
     * @return
     * @throws Exception
     */
    @Override
    public Result getComDistributionRule(ComDistributionRuleDto oComDistributionRuleDto) throws Exception {
        List<ComDistributionRule> oComDistributionRuleList = new ArrayList<>();
        long total = 0;
        QueryWrapper queryWrapper = new QueryWrapper();
        //判断是否传入等级
        if(oComDistributionRuleDto.getGrade() != null){
            queryWrapper.where(ComDistributionRuleTableDef.COM_DISTRIBUTION_RULE.GRADE.eq(oComDistributionRuleDto.getGrade()));
        }
        //判断是否传入类型
        if(oComDistributionRuleDto.getType() != null){
            queryWrapper.where(ComDistributionRuleTableDef.COM_DISTRIBUTION_RULE.TYPE.eq(oComDistributionRuleDto.getType()));
        }
        queryWrapper.orderBy("id desc");
        //是否分页
        if(oComDistributionRuleDto.getPage() != null && oComDistributionRuleDto.getLimit() != null){
            Page<ComDistributionRule> ComDistributionRulePage = oComDistributionRuleMapper.paginateWithRelations(oComDistributionRuleDto.getPage(),oComDistributionRuleDto.getLimit(),queryWrapper);
            oComDistributionRuleList = ComDistributionRulePage.getRecords();
            total = ComDistributionRulePage.getTotalRow();
        }else{
            oComDistributionRuleList = oComDistributionRuleMapper.selectListWithRelationsByQuery(queryWrapper);
            total = oComDistributionRuleList.size();
        }
        //PoToDto
        List<ComDistributionRuleDto> oComDistributionRuleDtoList = (List<ComDistributionRuleDto>) PoToDTO.poToDtoList(oComDistributionRuleList,new ComDistributionRuleDto());
        return Result.ok(oComDistributionRuleDtoList,total);
    }

    /**
     * 添加分销商等级规则
     * @param oComDistributionRuleDto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Result addComDistributionRule(ComDistributionRuleDto oComDistributionRuleDto) throws Exception {
        ComDistributionRule oComDistributionRule = new ComDistributionRule();
        PoToDTO.poToDto(oComDistributionRuleDto,oComDistributionRule);
        if(oComDistributionRuleMapper.insert(oComDistributionRule) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("添加分销商等级规则失败");
        }
        return Result.ok("添加分销商等级规则成功");
    }

    /**
     * 修改分销商等级规则
     * @param oComDistributionRuleDto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Result updateComDistributionRule(ComDistributionRuleDto oComDistributionRuleDto) throws Exception {
        ComDistributionRule oComDistributionRule = new ComDistributionRule();
        PoToDTO.poToDto(oComDistributionRuleDto,oComDistributionRule);
        if(oComDistributionRuleMapper.update(oComDistributionRule) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("修改分销商等级规则失败");
        }
        return Result.ok("修改分销商等级规则成功");
    }

    /**
     * 删除分销商等级规则
     * @param oComDistributionRuleDto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Result deleteComDistributionRule(ComDistributionRuleDto oComDistributionRuleDto) throws Exception {
        ComDistributionRule oComDistributionRule = new ComDistributionRule();
        PoToDTO.poToDto(oComDistributionRuleDto,oComDistributionRule);
        if(oComDistributionRuleMapper.deleteById(oComDistributionRule.getId()) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("删除分销商等级规则失败");
        }
        return Result.ok("删除分销商等级规则成功");
    }

}
